package com.example.hotel.service;

import com.hotel.management.model.Room;
import com.example.hotel.entity.RoomBooking;

import java.util.ArrayList;
import java.util.List;

public class RoomService {
    private List<Room> rooms = new ArrayList<>();
    private List<RoomBooking> bookings = new ArrayList<>();

    public RoomService() {
        rooms.add(new Room(1, "Single", true));
        rooms.add(new Room(2, "Double", true));
    }

    // 返回所有可用房间
    public List<Room> getAvailableRooms() {
        List<Room> availableRooms = new ArrayList<>();
        for (Room room : rooms) {
            if (room.isAvailable()) {
                availableRooms.add(room);
            }
        }
        return availableRooms;
    }

    // 创建预订
    public String bookRoom(int roomId, String guestName, String checkInDate, String checkOutDate) {
        // 检查房间是否存在
        Room roomToBook = null;
        for (Room room : rooms) {
            if (room.getId() == roomId) {
                roomToBook = room;
                break;
            }
        }

        if (roomToBook == null) {
            return "Room " + roomId + " does not exist.";
        }

        // 检查房间是否已预订
        if (!roomToBook.isAvailable()) {
            return "Room " + roomId + " is already booked.";
        }

        // 创建房间预订
        RoomBooking booking = new RoomBooking(bookings.size() + 1, roomId, guestName, checkInDate, checkOutDate);
        bookings.add(booking);
        roomToBook.setAvailable(false);  // 房间变为不可用
        return "Room " + roomId + " booked successfully for " + guestName + " from " + checkInDate + " to " + checkOutDate;
    }

    public String bookRoom2(int roomId, String guestName, String checkInDate, String checkOutDate) {
        for (Room room : rooms) {
            if (room.getId() == roomId) {
                if (!room.isAvailable()) {
                    return "Room " + roomId + " is already booked.";
                } else {
                    // 创建房间预订
                    RoomBooking booking = new RoomBooking(bookings.size() + 1, roomId, guestName, checkInDate, checkOutDate);
                    bookings.add(booking);
                    room.setAvailable(false);  // 房间变为不可用
                    return "Room booked successfully for " + guestName;
                }
            }
        }
        return "Room " + roomId + " not found.";
    }

}
